package pers.vinsonws.jianghu.serverspring.channel;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import pers.vinsonws.jianghu.codeCompont.MsgEveryPro;
import pers.vinsonws.jianghu.serverspring.handers.ChatServerHandler;
import pers.vinsonws.jianghu.serverspring.service.UserServiceImpl;


@Component
@Qualifier("ChatChannelInitializer")
public class ChatChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private ChatServerHandler chatServerHandler;

    public ChatServerHandler getChatServerHandler() {
        return chatServerHandler;
    }

    public void setChatServerHandler(ChatServerHandler chatServerHandler) {
        this.chatServerHandler = chatServerHandler;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //protobuf
        pipeline.addLast(new ProtobufVarint32FrameDecoder())
                .addLast(new ProtobufDecoder(MsgEveryPro.Message.getDefaultInstance()))
                .addLast(new ProtobufVarint32LengthFieldPrepender())
                .addLast(new ProtobufEncoder());
        pipeline.addLast(chatServerHandler);

    }



}
